package prisd.brand.com.base64;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;

import com.orhanobut.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import prisd.brand.com.base64.utils.RSAUtils;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        String abc = method1("abc");// 8e8vUYOQlUpvK7jsu3GLAw==
//        Logger.w("方法1：" + method1("abc") + "");
//        String val1 = "g5mSobIkvdlQkRKuCfMwePaA2wXdcYv7R6ENxe6UPmRxXdPYnWgIu00sSfDWpcsMwXTP9ZVQqY9xVA28eoBrvHhPHwAFAEJFKXDLLkdtHyXenoCeaWjnTIUx8zEyutwQ+ZGQAILBv8Snwb0XOgpvgrJfes76T0jLFjROmX3vyJs=";
//        String result = method2(val1);
//        Logger.i("解码结果：" + result + "");
        String method1 = method1(RSAUtils.PRIVATE_KEY);
        Logger.w("method1: "+method1);
    }

    public static String method1(String val) {
        try {
            SecretKeySpec localSecretKeySpec = new SecretKeySpec("13ed457a49931224dc6dc20fd71ac7f4".getBytes("UTF-8"), 0, "13ed457a49931224dc6dc20fd71ac7f4".getBytes("UTF-8").length, "AES");
            Cipher localCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            byte[] encoded = localSecretKeySpec.getEncoded();
            Logger.i("localSecretKeySpec: " + Arrays.toString(encoded) + " " + encoded);
            localCipher.init(1, localSecretKeySpec);
            return Base64.encodeToString(localCipher.doFinal(val.getBytes("UTF-8")), 0).replaceAll("\r|\n", "");
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    private String method2(String val) {
        try {
            String jsonData = "13ed457a49931224dc6dc20fd71ac7f4";
            SecretKeySpec localSecretKeySpec = new SecretKeySpec(jsonData.getBytes("UTF-8"), 0, jsonData.getBytes("UTF-8").length, "AES");
            Cipher localCipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            localCipher.init(2, localSecretKeySpec);//Base64.DEFAULT
            byte[] decode = Base64.decode(val, 0);
            byte[] bytes = localCipher.doFinal(decode);
            return new String(bytes, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void test1(){
        try {
            String jsonData = "13ed457a49931224dc6dc20fd71ac7f4";
            SecretKeySpec localSecretKeySpec = new SecretKeySpec(jsonData.getBytes("UTF-8"), 0, jsonData.getBytes("UTF-8").length, "AES");
            Logger.i("localSecretKeySpec:" + localSecretKeySpec.toString());
            String format = localSecretKeySpec.getFormat();
            Logger.w("format: "+format);
            String algorithm = localSecretKeySpec.getAlgorithm();
            Logger.i("algorithm: "+algorithm);
            byte[] encoded = localSecretKeySpec.getEncoded();
            String encodedResult = Arrays.toString(encoded);
            Logger.i("encodedResult: "+encodedResult +"  encoded.length: "+encoded.length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}
